package ua.com.minus1.lookbattle;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.actionbarsherlock.app.SherlockDialogFragment;
import ua.com.minus1.lookbattle.models.Country;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Igorek
 * Date: 11/3/12
 * Time: 9:37 PM
 * To change this template use File | Settings | File Templates.
 */
public class CountrySelectorDialogFragment extends SherlockDialogFragment implements AdapterView.OnItemClickListener {
    private ListView listView;
    private ArrayAdapter<Country> listAdapter;
    private Callbacks callbacks;

    public static CountrySelectorDialogFragment newInstance() {
        return new CountrySelectorDialogFragment();
    }

    public interface Callbacks {
        void onCountrySelect(Country country);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        //callbacks = (Callbacks) activity;
    }

    public void setCallbacks(Callbacks callbacks) {
        this.callbacks = callbacks;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        listView = new ListView(inflater.getContext());
        listView.setOnItemClickListener(this);
        if(listAdapter != null)
            listView.setAdapter(listAdapter);
        return listView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        List<Country> countries = CountriesFactory.getCountries(getActivity());
        listAdapter = new ArrayAdapter<Country>(getActivity(), android.R.layout.simple_spinner_dropdown_item, countries);
        if(listView != null)
            listView.setAdapter(listAdapter);
    }

    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        callbacks.onCountrySelect(listAdapter.getItem(i));
        dismiss();
    }
}
